package com.geek.novel.ui;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import com.geek.novel.R;
import com.geek.novel.common.Constant;
import com.geek.novel.network.ImageDownloader;
import com.geek.novel.utils.CommUtil;
import com.geek.novel.utils.LogCatUtil;

/**
 * 书籍的圆角图片
 * Created by  on 2017/8/22.
 */

public class BookImageView extends androidx.appcompat.widget.AppCompatImageView {

    private Paint paint;

    private float imageRadius;

    private String bookName = null;

    /**图片id*/
    private String imageId = null;

    public BookImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        this.imageRadius = CommUtil.INSTANCE.dip2px(5F);
    }

    /**
     * 获取圆形图片方法
     * @param bitmap    位图
     * @return Bitmap
     * @author caizhiming
     */
    private Bitmap getImageBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(output);
        canvas.drawARGB(0, 0, 0, 0);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);

        //这里，应该绘制圆角
        RectF rectF = new RectF(0, 0, width, height);
        canvas.drawRoundRect(rectF, imageRadius, imageRadius, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        Rect rect = new Rect(0, 0, width, height);
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    Bitmap bitmap = null;

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if(drawable == null){
            //此处应该绘制默认图片
            return;
        }
        Bitmap originalImage = ((BitmapDrawable) drawable).getBitmap();
        bitmap = getImageBitmap(originalImage);

        float bitmapWidth = bitmap.getWidth();
        float bitmapHeight = bitmap.getHeight();

        int width = getWidth();
        int height = getHeight();
        float scaleW = width / bitmapWidth;
        float scaleH = height / bitmapHeight;

        Matrix transformation = new Matrix();
        transformation.preScale(scaleW, scaleH);

        canvas.drawBitmap(bitmap, transformation, paint);
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getImageId() {
        return this.imageId;
    }

    public String getImageUrl(String imgId){
        return Constant.HTTP_IMAGE_URL + imgId;
    }

    public void loadBookImage(String imgId){
        this.loadBookImage(imgId, 0, 0);
    }

    public void loadBookImage(String imgId, float width, float height) {
        this.imageId = imgId;
        int errorImg = R.drawable.book_image_loading;
        if(TextUtils.isEmpty(imgId)){
            //绘制出错图片
            LogCatUtil.INSTANCE.d("???", "获取图片id为空，显示默认图片");
            this.setImageResource(errorImg);
        }else{
            String imageUrl = this.getImageUrl(imgId);
            //加载图片
            ImageDownloader downloader = ImageDownloader.Companion.getInstance(getContext());
            ImageDownloader.Builder builder = downloader.build(imageUrl)
                    .placeholder(errorImg)
                    .error(errorImg);
            //适配宽度高度
            if(width != 0 && height != 0){
                builder.resize(width, height);
            }else{
                builder.fit();
            }
            builder.into(this);
        }
    }
}
    
